package com.psa.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.psa.element.Customer;
import com.psa.system.*;
import com.psa.tool.SystemStatus;
import com.wxw.BaseLibrary.drawable.PanelStyle;

/**
 * Created by david on 14-3-31.
 */
public class CustomerListAdapter extends BaseAdapter {
    private Context _context=null;
    private LayoutInflater _inflater = null;
    private int index = -1;

    public CustomerListAdapter(Context context){
        _context = context;
        _inflater = LayoutInflater.from(_context);
    }
    @Override
    public int getCount() {
        return SystemStatus.currentCustomers==null?0:SystemStatus.currentCustomers.size();
    }

    @Override
    public Object getItem(int i) {
        return SystemStatus.currentCustomers==null || i<0 || i>=SystemStatus.currentCustomers.size()?null:SystemStatus.currentCustomers.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        HelperClass helperClass = null;
        Customer customer = SystemStatus.currentCustomers.get(i);
        if (convertView == null){
            convertView = _inflater.inflate(R.layout.customer_list_item, null);
            helperClass = new HelperClass();
            helperClass.tvTitle = (TextView)convertView.findViewById(R.id.tv_customer_title);
            helperClass.tvInfo = (TextView)convertView.findViewById(R.id.tv_customer_info);
            convertView.setTag(helperClass);
        } else {
            helperClass = (HelperClass)convertView.getTag();
        }
        if (i == index)
            convertView.setBackground(PanelStyle.createSkyBluePanelStyle());
        else
            convertView.setBackgroundColor(Color.WHITE);
        helperClass.tvTitle.setText(customer.getChineseName()+"("+customer.getEnglishName()+")");
        helperClass.tvInfo.setText(customer.getChineseAddress()+"["+customer.getContact() + ","+customer.getTelephone()+"]");
        return convertView;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
        notifyDataSetChanged();
    }

    class HelperClass{
        public TextView tvTitle;
        public TextView tvInfo;
    }
}
